<?php 

class shop
{
	/***************  VARIABLES STATIQUES ********************/
	
	
	/***************  FONCTIONS STATIQUES ********************/
	public static function getCatItems($catID)
	{
		$sql = core::getWebSQL();
		$query = "SELECT SQL_CACHE * FROM site_store_items WHERE id_cat = '".$sql->real_escape_string($catID)."' ORDER BY commentaires ASC";
		$stmt = $sql->query($query);
		if(!$stmt->num_rows) return false;
		else 
		{
			$tab = NULL;
			while($tmp = $stmt->fetch_object())
			{
				$tab[] = $tmp;
			}
			
			return $tab;
		}
	}
	
	public static function containsItem($item)
	{
		$tmpItem = new ShopItem();
		if($tmpItem->getByKey($item)) return true ;
		else return false;
	}
	public static function saveOrder($character)
	{
		$Basket = Basket :: getInstance();
		$strOrder = $Basket->getSerializedForm();
		$visiteur = core::getVisiteur();
		$user = $visiteur -> getUser();
		$order= new SiteShopOrder();
		$order->create();
		$order->lock();
		$order->account = $user->id;
		$order->order =$strOrder;
		$order->date = time();
		$order->character = $character;
		$order->unlock_attribute("date");
		$order->unlock_attribute("character");
		$order->unlock_attribute("account");
		$order->unlock_attribute("order");
		$order->write();
		
		return $order->id;
		
	}
	public static function sendOrderInGameBuffer($character)
	{
		//on doit créer des commandes en boucle dans la table du jeu
		$Basket = Basket :: getInstance();
		$items = $Basket->getItems();
		$visiteur = core::getVisiteur();
		$user = $visiteur -> getUser();
		if(is_array($items))
		{
			foreach($items as $it)
			{
				$GameShopOrder = new GameShopOrder();
				$GameShopOrder->create();
				$GameShopOrder->unlock();
				$GameShopOrder->character = $character;
				$GameShopOrder->type = $it['TYPE'];
				$GameShopOrder->ref = $it['ITEM'];
				$GameShopOrder->qty = $it['QTY'];
				$GameShopOrder->delivered = 0;
				$GameShopOrder->lock_attribute("id");
				$GameShopOrder->write();
				
				
			}
		}
	}
	public static function getStoreMainCats()
	{
		$sqlSite = core::getWebSQL();
		$SQL = $sqlSite->query("SELECT * FROM site_store_categories WHERE id_parent = 0");
		$tabCat = NULL;
		while($tmp = $SQL->fetch_object())
		{
			$tabCat[] = $tmp;
		}
		if(is_array($tabCat))
		{
			return $tabCat;
		}
		else
		{
			return -1;
		}
		
	}

	public static function getStoreSubCats($Cat)
	{
		$sqlSite = core::getWebSQL();;
		$SQL = $sqlSite->query("SELECT * FROM site_store_categories WHERE id_parent = ".$Cat." ");
		$tabCat = NULL;
		while($tmp = $SQL->fetch_object())
		{
			$tabCat[] = $tmp;
		}
		if(is_array($tabCat))
		{
			return $tabCat;
		}
		else
		{
			return -1;
		}
		
	}
	public static function getHTMLCatLinksList()
	{
		$sql = core::getWebSQL();
		$query = "SELECT SQL_CACHE * FROM site_store_categories ORDER BY id_parent ASC, cat_name ASC";
		
		$mainCats = self::getStoreMainCats();
		$list ="";
		if(is_array($mainCats))
		{
			foreach($mainCats as $Mcat)
			{
				$list .= "<a href='javascript:;' style='color:white;' id='cat-".$Mcat->id."'>".utf8_encode($Mcat->cat_name)."</a>";
				$subCats = shop::getStoreSubCats($Mcat->id);
				if(is_array($subCats))
				{
					foreach($subCats as $Scat)
					{
						$list .= "<a href='javascript:;' style='padding-left:20px' class='subcatcat' id='cat-".$Scat->id."'>".utf8_encode($Scat->cat_name)."</a>";
					}
				}
			}
			return $list;
		}
		else
		{
			return "<a href='javascript:;' class='white'>Aucune catégorie présente</a>";
		}
		
		
		
	}
	
	
}


?>